package com.woniuxy.project.campus.technical.config;


import com.woniuxy.project.campus.AccountOpenFeign;
import com.woniuxy.project.campus.technical.utilTool.MyTool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import javax.annotation.Resource;

@Configuration
@EnableWebSocket
@Component
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private AccountOpenFeign accountOpenFeign;
    @Resource
    private MyTool myTool;


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册 socket处理器
        registry.addHandler(webSocketUtil(), "/web-socket/{token}").setAllowedOrigins("*");
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 消息缓冲区大小的大小
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }

    @Bean
    public WebSocketHandler webSocketUtil() {
        WebSocketUtil webSocketUtil = new WebSocketUtil();
        webSocketUtil.setAccountOpenFeign(accountOpenFeign);
        webSocketUtil.setMyTool(myTool);
        return webSocketUtil;
    }
}